package edu.gdpu.classloader;

/**
 * @author mazebin
 * @date 2021年 07月08日 10:54:35
 */
public class ClassLoaderTest17_2 {
    public static void main(String[] args) throws Exception {
        CustomClassLoader loader = new CustomClassLoader("loader");

        loader.setPath("D:\\testClass\\");
        Class<?> clazz = loader.loadClass("edu.gdpu.classloader.MySimple2");
        System.out.println(clazz.hashCode());
        //如果注释掉该行，就并不会实例化MySample2对象，不会加载MySecSimple（可能预先加载）
        Object instance = clazz.newInstance();//实列化Simple和MyCat
        //删除classpath下的MySimple2.class，留下MySecSimple2，程序报错
        //因为命名空间，父加载器找不到子加载器所加载的类，因此MySecSimple2找不到
        //输出结果：
        /**
         * findClass,输出这句话说明我们自己的类加载器加载了指定的类
         * 1173230247
         * MySimple by Load edu.gdpu.classloader.CustomClassLoader@6d6f6e28
         * MySecSimple2 by load sun.misc.Launcher$AppClassLoader@18b4aac2
         * Exception in thread "main" java.lang.NoClassDefFoundError: edu/gdpu/classloader/MySimple2
         * 	at edu.gdpu.classloader.MySecSimple2.<init>(MySecSimple2.java:6)
         * 	at edu.gdpu.classloader.MySimple2.<init>(MySimple2.java:11)
         * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
         * 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
         * 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
         * 	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
         * 	at java.lang.Class.newInstance(Class.java:442)
         * 	at edu.gdpu.classloader.ClassLoaderTest17_2.main(ClassLoaderTest17_2.java:15)
         * Caused by: java.lang.ClassNotFoundException: edu.gdpu.classloader.MySimple2
         * 	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
         * 	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
         * 	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
         * 	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
         * 	... 8 more
         *
         */

    }

}




